﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using Facebook;
using System.Text;
using System.Diagnostics;

namespace PhoneAppSTS
{
    public partial class FacebookLog : PhoneApplicationPage
    {
        private string _appID = "300822266638830";
        private string _appSecret = "41834c9626f34d72d61f5409a61682f2";
        private string _accessToken;

        public FacebookLog()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            StartFacebookLogin();

        }


        private void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            webBrowser.Navigated += new EventHandler<NavigationEventArgs>(webBrowser_Navigated);
        }

        void webBrowser_Navigated(object sender, NavigationEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void StartFacebookLogin()
        {
            string[] extendedPermissions = new[] { "user_about_me", "publish_stream", "offline_access" };

            var oauth = new FacebookOAuthClient { AppId = _appID, AppSecret = _appSecret };
            var parameters = new Dictionary<string, object>
                            {
                                {"response_type", "token"},
                                {"display", "touch"}                                        
                            };
            if (extendedPermissions != null && extendedPermissions.Length > 0)
            {
                var scope = new StringBuilder();
                scope.Append(string.Join(",", extendedPermissions));
                parameters["scope"] = scope.ToString();
            }

            var loginUrl = oauth.GetLoginUrl(parameters);

            webBrowser.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(CheckForAuth);


            webBrowser.Navigate(loginUrl);
        }

        private void CheckForAuth(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            FacebookOAuthResult result;
            if (FacebookOAuthResult.TryParse(e.Uri, out result))
            {
                if (result.IsSuccess)
                {
                    _accessToken = result.AccessToken;
                    Debug.WriteLine(result.AccessToken);
                }
            }

        }

        private void LogoutFromFacebook()
        {
            WebBrowser webBrowserLogout = new WebBrowser();
            webBrowserLogout.Visibility = Visibility.Collapsed;
            webBrowserLogout.Navigate(new Uri("https://m.facebook.com/logout.php?next=http://m.facebook.com/&access_token=" + _accessToken, UriKind.Absolute));
            webBrowserLogout.LoadCompleted += new LoadCompletedEventHandler(webBrowserLogout_LoadCompleted);
        }

        private void webBrowserLogout_LoadCompleted(object sender, NavigationEventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}